package com.igd.utils;

import java.math.BigDecimal;

import org.apache.commons.lang3.StringUtils;

/**
 * @createDate 2013-7-18
 * 
 * @author QIAOYU
 * 
 * @version 1.0
 * 
 * @description 金额操作工具类
 */

public class AmountUtil {
	
	private final static String fraction[] = {"角","分"};
	
	private final static String digit[] = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
	
	private final static String unit[][] = {{ "元", "万", "亿" },{ "", "拾", "佰", "仟" }};
	
	private final static String negative = "负";
	
	private final static String zero = "零";
	
	private final static String entire = "整";
	
	private final static String yuan = "元";
	
	/**
	 * 将传入的字符串转换为中文大写金额
	 * 
	 * @param n
	 * 
	 * @return
	 */
	public static String toChineseAmount(String n) {

		if(StringUtils.isNotEmpty(n)){
			
			double number = Double.valueOf(n);
			
			String head = number < 0 ? negative:"";

			number = Math.abs(number);

			String s = "";

			for (int i = 0; i < fraction.length; i++) {

				s += (digit[(int) (Math.floor(number * 10 * Math.pow(10, i)) % 10)] + fraction[i]).replaceAll("("+zero+".)+","");

			}

			if (s.length() < 1) {

				s = entire;

			}

			int integerPart = (int) Math.floor(number);

			for (int i = 0; i < unit[0].length && integerPart > 0; i++) {

				String p = "";

				for (int j = 0; j < unit[1].length && number > 0; j++) {

					p = digit[integerPart % 10] + unit[1][j] + p;

					integerPart = integerPart / 10;

				}

				s = p.replaceAll("("+zero+".)*"+zero+"$", "").replaceAll("^$",zero) + unit[0][i] + s;

			}
			
			return head + s.replaceAll("("+zero+".)*"+zero+"元",yuan).replaceFirst("("+zero+".)+", "").replaceAll("("+zero+".)+", zero).replaceAll("^"+entire+"$",zero.concat(yuan).concat(entire));
			
		}
		
		return null;
	}
	
	/**
	 * 金额相加方法,传入的金额以分为单位
	 * 
	 * @param num
	 * 
	 * @param num1
	 * 
	 * @return
	 */
	public static String add(String num,String num1){

		if(StringUtils.isNotEmpty(num) && StringUtils.isNotEmpty(num1)){
			
			BigDecimal numBd = new BigDecimal(num);
			
			BigDecimal num1Bd = new BigDecimal(num1);

			return numBd.add(num1Bd).toString();

		}
		
		return null;
	}
	
	/**
	 * 金额相减方法,传入的金额以分为单位
	 * 
	 * @param num
	 * 
	 * @param num1
	 * 
	 * @return
	 */
	public static String subtraction(String num,String num1){

		if(StringUtils.isNotEmpty(num) && StringUtils.isNotEmpty(num1)){
			
			BigDecimal numBd = new BigDecimal(num);
			
			BigDecimal num1Bd = new BigDecimal(num1);

			return numBd.subtract(num1Bd).toString();

		}
		
		return null;
	}
	
	/**
	 * 金额相乘方法,传入的金额以分为单位
	 * 
	 * @param num
	 * 
	 * @param num1
	 * 
	 * @return
	 */
	public static String multiply(String num,String num1){

		if(StringUtils.isNotEmpty(num) && StringUtils.isNotEmpty(num1)){
			
			BigDecimal numBd = new BigDecimal(num);
			
			BigDecimal num1Bd = new BigDecimal(num1);

			return numBd.multiply(num1Bd).toString();

		}
		
		return null;
	}
	
	/**
	 * 金额相除方法,传入的金额以分为单位
	 * 
	 * @param num
	 * 
	 * @param num1
	 * 
	 * @return
	 */
	public static String divide(String num,String num1){
		
		if(StringUtils.isNotEmpty(num) && StringUtils.isNotEmpty(num1)){
			
			BigDecimal numBd = new BigDecimal(num);
			
			BigDecimal num1Bd = new BigDecimal(num1);

			return numBd.divide(num1Bd,BigDecimal.ROUND_HALF_UP).toString();

		}
		
		return null;
	}
	
	/**
	 * 金额相除方法,传入的金额以分为单位
	 * 
	 * @param num
	 * 
	 * @param num1
	 * 
	 * @return
	 */
	public static String divide(String num,String num1,int len){
		
		if(StringUtils.isNotEmpty(num) && StringUtils.isNotEmpty(num1)){
			
			BigDecimal numBd = new BigDecimal(num);
			
			BigDecimal num1Bd = new BigDecimal(num1);

			return numBd.divide(num1Bd,len,BigDecimal.ROUND_HALF_UP).toString();

		}
		
		return null;
	}
	
	/**
	 * 分转换为元,保留两位小数
	 * 
	 * @param amountStr
	 * 
	 * @return
	 */
	public static String fenToYuan(String amountStr){
		
		if(StringUtils.isNotEmpty(amountStr)){
			
			return divide(amountStr,"100",2);
			
		}
		
		return null;
	}
	
	/**
	 * 元转换为分
	 * 
	 * @param amountStr
	 * 
	 * @return
	 */
	public static String yuanToFen(String amountStr){
		
		if(StringUtils.isNotEmpty(amountStr)){
			
			return multiply(amountStr,"100");
			
		}
		
		return null;
	}
}
